﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hello_accinv_New_
{
    public partial class LoginForm : Form
    {
        private string loginName;
        private int loginPermission;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DoLogin();
        }

        private void DoLogin()
        {
            if (IsValidLogin())
            {
                Visible = false;
                MainForm main = new MainForm();
                if (main.ShowDialog() == System.Windows.Forms.DialogResult.Retry)
                {
                    // Retry = Logout
                    // Abort = Exit
                    Visible = true;
                    GC.Collect();
                }
                else
                {
                    Dispose();
                }
            }
        }
        
        private bool IsValidLogin()
        {
            bool valid;

            if (txtboxUsername.Text.Length > 0 && txtboxPassword.Text.Length > 0)
            {
                // Check from database
                Database db = new Database();
                DataTable dt = db.Select("Firstname, Lastname, Permission", "Users", "Username = '" + txtboxUsername.Text + "' AND Passwd = '" + txtboxPassword.Text + "'");
                if (dt.Rows.Count == 1)
                {
                    loginName = dt.Rows[0]["Firstname"] + " " + dt.Rows[0]["Lastname"];
                    loginPermission = Int32.Parse(dt.Rows[0]["Permission"].ToString());

                    GlobalClass.userName = txtboxUsername.Text;
                    GlobalClass.permission = loginPermission;
                    GlobalClass.fullName = loginName;

                    txtboxUsername.Clear();
                    txtboxPassword.Clear();

                    valid = true;
                }
                else
                {
                    MessageBox.Show("Wrong username or password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    valid = false;
                }
            }
            else
            {
                MessageBox.Show("Please enter username and password", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                valid = false;
            }

            return valid;
        }

        private void txtboxUsername_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                DoLogin();
            }
        }

        private void txtboxPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                DoLogin();
            }
        }
    }
}
